package controllers

import com.jfinal.aop.Before
import com.jfinal.core.Controller
import controllers.interceptors.UserTokenCheckValidator
import services.OrderService

@Before(UserTokenCheckValidator::class)
class OrderController: Controller() {

    companion object {
        const val RECORD_ID = "record_id"
        const val TO_ADDR = "to_addr"
        const val ORDER_ID = "order_id"
    }

    private val service: OrderService by lazy {
        OrderService(this)
    }

    fun create() {
        val userId = UserTokenCheckValidator.getUserId(this)
        val recordId = getInt(RECORD_ID)
        val toAddr = get(TO_ADDR)
        service.createNewOrder(userId, recordId, toAddr)
    }

    fun finish(){
        val userId = UserTokenCheckValidator.getUserId(this)
        val orderId = getInt(ORDER_ID)
        service.finishOrder(userId, orderId)
    }

    fun cancel(){
        val userId = UserTokenCheckValidator.getUserId(this)
        val orderId = getInt(ORDER_ID)
        service.cancelOrder(userId, orderId)
    }

    fun pay(){
        val userId = UserTokenCheckValidator.getUserId(this)
        val orderId = getInt(ORDER_ID)
        service.payOrder(userId, orderId)
    }

    fun list(){
        val userId = UserTokenCheckValidator.getUserId(this)
        service.listAllOrders(userId)
    }


}